package com.galgram.project.controller;

import java.util.List;
import java.util.ArrayList;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import com.galgram.common.annotation.Log;
import com.galgram.common.core.controller.BaseController;
import com.galgram.common.core.domain.AjaxResult;
import com.galgram.common.enums.BusinessType;
import com.galgram.project.domain.FxPoints;
import com.galgram.project.service.IFxPointsService;
import com.galgram.common.utils.poi.ExcelUtil;
import com.galgram.common.core.page.TableDataInfo;

/**
 * 积分Controller
 *
 * @author galgram
 * @date 2024-11-20
 */
@RestController
@RequestMapping("/project/points")
public class FxPointsController extends BaseController {
    @Autowired
    private IFxPointsService fxPointsService;

    /**
     * 查询积分列表
     */
    @GetMapping("/getList")
    public TableDataInfo getPointsList(FxPoints fxPoints) {
        startPage();
        List<FxPoints> list = fxPointsService.getPointsList(fxPoints);
        return getDataTable(list);
    }

    /**
     * 查询积分列表
     */
    @PreAuthorize("@ss.hasPermi('project:points:list')")
    @GetMapping("/list")
    public TableDataInfo list(FxPoints fxPoints) {
        startPage();
        List<FxPoints> list = fxPointsService.selectFxPointsList(fxPoints);
        return getDataTable(list);
    }

    /**
     * 获取积分详细信息
     */
    @PreAuthorize("@ss.hasPermi('project:points:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(fxPointsService.getById(id));
    }

    /**
     * 新增积分
     */
    @PreAuthorize("@ss.hasPermi('project:points:add')")
    @Log(title = "积分", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FxPoints fxPoints) {
        fxPointsService.save(fxPoints);
        return success("添加成功");
    }

    /**
     * 修改积分
     */
    @PreAuthorize("@ss.hasPermi('project:points:edit')")
    @Log(title = "积分", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FxPoints fxPoints) {
        fxPointsService.updateById(fxPoints);
        return success("修改成功");
    }

    /**
     * 删除积分
     */
    @PreAuthorize("@ss.hasPermi('project:points:remove')")
    @Log(title = "积分", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<Long> ids) {
        fxPointsService.removeByIds(ids);
        return success("删除成功");
    }


    /**
     * 导出积分列表
     */
    @PreAuthorize("@ss.hasPermi('project:points:export')")
    @Log(title = "积分", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, FxPoints fxPoints) {
        List<FxPoints> list = fxPointsService.selectFxPointsList(fxPoints);
        ExcelUtil<FxPoints> util = new ExcelUtil<FxPoints>(FxPoints.class);
        util.exportExcel(response, list, "积分数据");
    }

    /**
     * 导入积分列表
     */
    @PreAuthorize("@ss.hasPermi('project:points:import')")
    @Log(title = "积分", businessType = BusinessType.IMPORT)
    @PostMapping("/import")
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<FxPoints> util = new ExcelUtil<FxPoints>(FxPoints.class);
        List<FxPoints> userList = util.importExcel(file.getInputStream());
        fxPointsService.saveBatch(userList);
        return AjaxResult.success("导入成功");
    }

    /**
     * 下载模版
     */
    @PreAuthorize("@ss.hasPermi('project:points:export')")
    @PostMapping("/importTemplate")
    public void exportTemplate(HttpServletResponse response) {
        List<FxPoints> list = new ArrayList<FxPoints>();
        ExcelUtil<FxPoints> util = new ExcelUtil<FxPoints>(FxPoints.class);
        util.exportExcel(response, list, "积分数据");
    }
}
